package com.yvtian.inv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.admin.pojo.SysDept;
import com.yvtian.inv.dao.InvSortMapper;
import com.yvtian.inv.pojo.InvSortEntity;
import com.yvtian.inv.pojo.ShopEntity;
import com.yvtian.inv.dao.ShopMapper;
import com.yvtian.inv.service.ShopService;
import com.yvtian.pu.pojo.PuApplyForEntity;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (shop)表服务实现类
 *
 * @author long
 * @since 2024-04-10 15:26:28
 */
@Service("shopService")
public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopEntity> implements ShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private InvSortMapper invSortMapper;

    @Override
    public IPage<ShopEntity> findByPage(Page<ShopEntity> page, ShopEntity shopEntity) {
        LambdaQueryWrapper<ShopEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopEntity::getIsDel, 0);
        if (StringHelper.isNotEmpty(shopEntity.getShopName())) {
            queryWrapper.like(ShopEntity::getShopName, shopEntity.getShopName());
        }
        if (StringHelper.isNotEmpty(shopEntity.getShopCode())) {
            queryWrapper.like(ShopEntity::getShopCode, shopEntity.getShopCode());
        }
        if (shopEntity.getQuantity() != null){
            queryWrapper.eq(ShopEntity::getQuantity, shopEntity.getQuantity());
        }
        if (shopEntity.getSortId() != null){
            ArrayList<InvSortEntity> invSortEntities = invSortMapper.recursionInvSort(shopEntity.getSortId());
            List<Integer> sortId = invSortEntities.stream()
                    .map(InvSortEntity::getSortId)
                    .collect(Collectors.toList());
            queryWrapper.in(ShopEntity::getSortId,sortId);
        }
        if (StringHelper.isNotEmpty(shopEntity.getCreateName())) {
            queryWrapper.like(ShopEntity::getCreateName, shopEntity.getCreateName());
        }
        if (StringHelper.isNotEmpty(shopEntity.getUpdateName())) {
            queryWrapper.like(ShopEntity::getUpdateName, shopEntity.getUpdateName());
        }
        if (StringHelper.isNotEmpty(shopEntity.getCreateUser())) {
            queryWrapper.like(ShopEntity::getCreateUser, shopEntity.getCreateUser());
        }
        if (StringHelper.isNotEmpty(shopEntity.getRemarks())) {
            queryWrapper.like(ShopEntity::getRemarks, shopEntity.getRemarks());
        }
        return this.page(page, queryWrapper);
    }
}

